/*
 * 单个页面的值
 */

//import { combineReducers } from 'redux';
//import 'babel-polyfill';
import {getId,getImageWH,createNewPage} from '../../utils/H5helper';

import {addNewLabelComp,addNewImageComp,addNewTextInput,addNewTextarea,addNewSelectComp,updateComponent} from './componentHandle';

function pageDataUpdate(state,action){
	let components=state.get('components');
	switch (action.type) {
		//修改attrubutes属性
		case 'UPDATE_PAGE_ATTR':
			const attr=state.get('attributes').merge(action.pageAttr);
			return state.set('attributes',attr);
		//事件操作
	   	case 'UPDATE_PAGE_EVENTS':
	   		const newEvents=state.get('events').merge(action.pageAttr);
			return state.set('events',newEvents);
	   	//调整组件顺序
	   	case 'PAGE_CHANGE_COMP_ORDER':
	   		const targetCompNum=components.get(action.targetCompPos).get('compNum');
			const currentCompNum=components.get(action.sourceCompId).get('compNum');
			let upCompList=Immutable.List();
			components.forEach(comp=>{
				let newCompNum=comp.get('compNum');
				const compid=comp.get('id');
				if(targetCompNum>currentCompNum){//向下拖，中间的组件-1
					if(comp.get('compNum')>=currentCompNum && comp.get('compNum')<=targetCompNum){//只有在这个范围的数据才会变更
						if(compid==action.sourceCompId){//当前组件的顺序等于目标顺序
							newCompNum=targetCompNum;
						}else{
							newCompNum=newCompNum-1;
						}
						let newComp=comp.set('compNum',newCompNum);
						upCompList=upCompList.push(newComp);
					}
				}else{//向上拖，中间的组件+1
					if(comp.get('compNum')>=targetCompNum && comp.get('compNum')<=currentCompNum){
						if(compid==action.sourceCompId){
							newCompNum=targetCompNum;
						}else{
							newCompNum=newCompNum+1;
						}
						let newComp=comp.set('compNum',newCompNum);
						upCompList=upCompList.push(newComp);
					}
				}
			});
			let newChangeComponents=components;
			upCompList.forEach(item=>{
				newChangeComponents=newChangeComponents.set(item.get('id'),item);
			});
			return state.set('components',newChangeComponents);
	   	//新增文本框组件
	   	case 'PAGE_NEW_ADD_LABEL_COMPONENT':
	   		let newLabel=addNewLabelComp(components.size+1,action.text,action.background);
	   		let compsLabel=components.set(newLabel.get('id'),newLabel);
	   		return state.set('components',compsLabel);
	   	//新增图片组件
	   	case 'PAGE_NEW_ADD_IMAGE_COMPONENT':
	   		const res=getImageWH(action.imgSrc);
	   		let compsImage=addNewImageComp(components.size+1,action.imgSrc,action.imgName,res.width,res.height);
	   		return state.set('components',components.set(compsImage.get('id'),compsImage));
	   	//新增输入框组件
	   	case 'PAGE_NEW_ADD_TEXT_COMPONENT':
	   		let newText=addNewTextInput(components.size+1);
	   		let compsText=components.set(newText.get('id'),newText);
	   		return state.set('components',compsText);
	   	//新增多行文本框组件
	   	case 'PAGE_NEW_ADD_TEXTAREA_COMPONENT':
	   		let newTextarea=addNewTextarea(components.size+1);
	   		let compsTextarea=components.set(newTextarea.get('id'),newTextarea);
	   		return state.set('components',compsTextarea);
	   	//新增选择框组件
	   	case 'PAGE_NEW_ADD_SELECT_COMPONENT':
	   		let newSelect=addNewSelectComp(components.size+1);
	   		let compsSelect=components.set(newSelect.get('id'),newSelect);
	   		return state.set('components',compsSelect);
	   	//新增计数器插件

	   	//删除组件
	   	case 'PAGE_DELETE_COMPONENT':
	   		let compsDelete=components.delete(action.componentId);
	   		return state.set('components',compsDelete);

	   	default:
	   		//修改单个组件
	   		const {componentId}=action;
	   		if(componentId && components.get(componentId) ){
	   			const upComp=updateComponent(components.get(componentId),action);
	   			let ncomponents=components.set(componentId,upComp);
	   			return state.set('components',ncomponents);
	   		}
	   		//默认返回
	   		return state;
	}
}


//测试数据
//import {pagelist} from '../../testData/EditStagePage';
const blankPage=createNewPage(1);
const initList=Immutable.Map({[blankPage.get("id")]:blankPage});
function pageDatas(state=initList,action){
	switch (action.type) {
		case 'INIT_DATA':
			if(action.pagesData){
				const pagesObj = action.pagesData.reduce((obj, page) => {
					if(page){
						const componentsObj=page.components.reduce((obj2,comp)=>{
					  		if(comp) obj2[comp.id]=comp;
					  		return obj2;
					    },{});
			            obj[page.id] = page;
			            obj[page.id]['components']=componentsObj;
					}
		        	return obj
		        }, {});
				return Immutable.fromJS(pagesObj);
			}
			return initList;
		case 'NEW_ADD_PAGE'://新增页面
			const newPage=createNewPage(action.pageNum);
			let upPageList=Immutable.List();
			state.forEach(page=>{
				if(page.get('pageNum')>=action.pageNum){
					const newPage=page.set('pageNum',page.get('pageNum')+1);
					upPageList=upPageList.push(newPage);
				}
			});
			let pages=state;
			upPageList.forEach(item=>{
				pages=pages.set(item.get('id'),item);
			});
			return pages.set(newPage.get('id'),newPage);
		//删除一个页面
		case 'H5_PAGE_DELETE':
			//return state.delete(action.deletePageId);
			const deletePageNum=state.get(action.deletePageId).get('pageNum');
			const newDelPage=state.delete(action.deletePageId);
			//console.log(deletePageNum+'---'+state.size);
			if(deletePageNum==state.size){//删除的是最后一页的话
				return newDelPage;
			}
			let newRtPages=Immutable.Map();
			newDelPage.forEach(page=>{
				let currNum=page.get('pageNum');
				if(currNum>deletePageNum){//比当前删除的页码大的，页码都减1
					const newPage=page.set('pageNum',--currNum);
					newRtPages=newRtPages.set(page.get('id'),newPage);
				}else{
					newRtPages=newRtPages.set(page.get('id'),page);
				}
			});
			return newRtPages;
		case 'CHANGE_PAGE_NUMBER'://移动页面的位置
			const targetPageNum=state.get(action.targetPagePos).get('pageNum');
			const currentPageNum=state.get(action.sourcePageId).get('pageNum');
			let upPageList2=Immutable.List();
			state.forEach(page=>{
				const pageid=page.get('id');
				let newPageNum=page.get('pageNum');
				if(targetPageNum>currentPageNum){//向下拖，中间的页码-1
					if(page.get('pageNum')>=currentPageNum && page.get('pageNum')<=targetPageNum){
						if(pageid==action.sourcePageId){//当前页面的页面等于目标页码
							newPageNum=targetPageNum;
						}else{
							newPageNum=newPageNum-1;
						}
						const newPage=page.set('pageNum',newPageNum);
						upPageList2=upPageList2.push(newPage);
					}
				}else{//向上拖，中间的页码+1
					if(page.get('pageNum')>=targetPageNum && page.get('pageNum')<=currentPageNum){
						if(pageid==action.sourcePageId){//当前页面的页面等于目标页码
							newPageNum=targetPageNum;
						}else{
							newPageNum=newPageNum+1;
						}
						const newPage=page.set('pageNum',newPageNum);
						upPageList2=upPageList2.push(newPage);
					}
				}
			});
			let pages2=state;
			upPageList2.forEach(item=>{
				pages2=pages2.set(item.get('id'),item);
			});
			return pages2;
	   	default:
	   		const {pageId}=action;
	   		if(pageId && state.get(pageId) ){
	   			const newPage=pageDataUpdate(state.get(pageId), action);
	   			return state.set(pageId,newPage);
	   		}
		   	return state;
  	}
}

//export default combineReducers({pageDatas});
export default pageDatas;

